<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="javascript:void(0);">公司信息</a></el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card>
            <el-card class="CompanyInfo-box-card">
                <h2 class="Regi-title">公司信息</h2>
                <el-form :model="CompanyInfo" ref="form" label-width="100px">
                    <el-form-item label="公司名称：">
                        <el-input v-model="CompanyInfo.name" :disabled="disabled"></el-input>
                    </el-form-item>
                    <el-form-item label="所在城市：">
                        <el-input v-model="CompanyInfo.town" :disabled="disabled"></el-input>
                    </el-form-item>
                    <el-form-item label="所在街道：">
                        <el-input v-model="CompanyInfo.street" :disabled="disabled"></el-input>
                    </el-form-item>
                    <el-form-item label="公司Logo：">
                        <img :src="src" :alt="CompanyInfo.name" class="Company-Logo">
                    </el-form-item>
                    <el-form-item v-if="!disabled" >
                        <p>七牛云上传：<input type="file" @change="upload_qiniu"></p>
                    </el-form-item>
                    <el-form-item v-if="!disabled" style="text-align:center;">
                        <el-button type="primary" @click="SubmitUpdate">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </el-card>
    </div>
</template>

<script>
import { company_info_get, qi_niu_get, update_company_info_put } from '../axios_api/api'
import { config, formatXml } from '../../config'
export default {
    data() {
        return {
            // 公司信息
            CompanyInfo: {
                name: '',
                town: '',
                street: '',
                logo: '',
            },
            // 公司Logo
            src: '',
            // 是否可以修改
            disabled: '',
            // 七牛云token
            uptoken: '',
        }
    },
    mounted() {
        this.get_User_CompanyInfo();
        this.disabled = formatXml(this.disabled);
        this.get_uptoken();
    },
    methods: {
        // 提交修改公司信息
        SubmitUpdate() {
            update_company_info_put(this.CompanyInfo).then(resp => {
                if (resp.code === 200) {
										this.$message.success(resp.message)
										this.$router.go(0)
                } else {
                    this.$message.error(resp.message)
                }
            })
        },
        // 获取公司信息
        get_User_CompanyInfo() {
            let params = { token: localStorage.getItem('token') }
            company_info_get(params).then(resp => {
                this.CompanyInfo = resp.companyinfo
                // 判断是否为默认头像
                if (resp.companyinfo.logo === 'Logo.jpg') {
                    // 是则用本地路由
                    this.src = config['uploadurl'] + resp.companyinfo.logo
                    // 反之用七牛云路由
                } else {
                    this.src = config['qiniuurl'] + resp.companyinfo.logo
                }
                console.log(resp)
            })
        },
        // 获取凭证
        get_uptoken() {
            qi_niu_get().then(resp => {
                this.uptoken = resp.token
            })
        },
        // 上传七牛云
        upload_qiniu(e) {
            // 获取文件对象
            let file = e.target.files[0];
            // 声明参数
            let param = new FormData();
            //将上传凭证添加参数
            param.append('token', this.uptoken);
            // 附加文件
            param.append('file', file);
            // 定制化axios请求
            const axios_qiniu = this.axios.create({ withCredentials: false }); // 将cookkie关闭
            // 发送请求
            axios_qiniu({
                // 七牛云华北上传地址
                url: 'http://up-z1.qiniu.com/',
                method: 'post',
                data: param,
                timeout: 30000,
            }).then(resp => {
                console.log(resp);
                this.src = config['qiniuurl'] + resp.data.key
                this.CompanyInfo.logo = resp.data.key
            })
        },
    }
}
</script>

<style>
</style>